# @Time    : 2018/5/31 17:20
# @Author  : Jame
from db import models
from core import student as student_core
from interface import common_interface

#创建学生日志对象
logger=common_interface.load_my_logging_cfg('student')

#1.学生注册接口
def student_register_interface(name,password):
    obj = models.Student.select(name)
    if obj:  # 学生若存在
        return False, '%s 已存在，请重试...'%(name)
    else:
        student= models.Student() #
        student.register(name, password)

        logger.info('[%s]: %s 注册成功!'%(student_core.student_info['role'],name))
        return True, '%s 注册成功!' %name



#2.学生登陆接口--见interface-->common_interface


#3.学生选择学校
def student_select_school_interface(student_name,school_name):
    obj = models.Student.select(student_name)
    if obj.school:
        logger.error('[%s]:%s 选择校区失败，请勿重复选择'%(student_core.student_info['role'],school_name))
        return False, '%s 校区已经选择，请勿重复选择...' % school_name
    else:
        obj.select_school(school_name)

        logger.info('[%s]: %s 选择 %s 学校成功!' % (student_core.student_info['role'],student_name,school_name))
        return True,'%s 学校，选择成功!' %school_name



#4.学生选择课程
def student_select_course_interface(student_name,course_name):
    obj=models.Student.select(student_name)
    course_obj=models.Course.select(course_name)
    if course_name in obj.course_list or student_name in course_obj.student_name_list:
        return False,'%s 课程已经选择，请勿重复选择...'%course_name
    else:
        if course_obj.school_name==obj.school: #如果所选择课程在学生的校区
            obj.student_select_course(course_name)
            course_obj.add_student(student_name)

            logger.info('%s 课程，选择成功!'%course_name)
            return True,'%s 课程，选择成功!'%course_name
        else:
            return False,'%s 课程不在该校区，请联系管理员!'%course_name


#5.学生查看成绩
def student_check_score_interface(student_name):
    obj=models.Student.select(student_name)
    while True:
        for n,v in enumerate(obj.course_list):
            print('%s : %s'%(n,v))
        course_num=input('请选择课程对应的序号:').strip()
        if not course_num.isdigit():continue
        course_name=obj.course_list[int(course_num)]
        if obj.scores[course_name]==0:

            logger.debug('% 查询分数未显示，老师尚未评分'%student_name)
            return False,'老师尚未评分，请稍后再查看...'
        else:
            return True,obj.scores[course_name]



#6.学生交学费
def pay_cost_interface(name):
    sum=0
    student_obj=models.Student.select(name)

    for course in student_obj.course_list:
        course_obj=models.Course.select(course)
        sum+=int(course_obj.price)
    while True:
        print('您报的所有课程的费用为: %s'%sum,'您的余额为:%s'%(student_obj.balance),'还有 %s 的费用尚未交齐!'%(sum-student_obj.balance))
        if sum==student_obj.balance:
            logger.debug('[学生]:%s 费用已经交齐，请勿重复缴费!'%name)
            return  True,'[学生] :%s 费用已经交齐，请安心学习!'%name
        cost=input('请输入你要缴费的金额:').strip()
        if not cost.isdigit():continue
        cost = int(cost)
        student_obj.balance += cost
        print(student_obj.balance)
        if student_obj.balance ==sum:
            student_obj.pay_cost(student_obj.balance)
            logger.info('学生 %s 学费交齐.'%name)
            return True,'恭喜您，费用交齐，请安心上课吧.'
        else:
            student_obj.pay_cost(student_obj.balance)
            logger.debug('学生 %s,正在缴费，还有 %s 费用尚未交齐.'%(name,sum-student_obj.balance))
            return False,'已经交了%s ,学费还有 %s 未交齐'%(student_obj.balance,sum-student_obj.balance)



#7.修改我的密码
def modify_passwod_interface(student_name,newpassword):
    student_obj=models.Student.select(student_name)
    if student_obj.password==newpassword: #新旧密码一直，提示
        return False,'新密码和旧密码一致，修改无效，请重试!'
    else:
        student_obj.modify_password(newpassword)
        return True,'%s 的密码修改已生效,即将退出,请重新登陆!'%student_name